package com.example.aircraftwar.application;

import android.app.Service;
import android.content.Intent;
import android.content.res.AssetManager;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.example.aircraftwar.R;

import java.io.IOException;
import java.util.HashMap;

public class MusicService extends Service {
    private static final String TAG = "MusicService";
    private HashMap<String, Integer> soundID = new HashMap<String, Integer>();
    private SoundPool mSoundPool;
    private MediaPlayer player;
    private AssetManager aManager;
    public MusicService() {

    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "==== MusicService onCreate ===");
        aManager = getAssets();
        if (mSoundPool == null) {
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                AudioAttributes audioAttributes = null;
                audioAttributes = new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_MEDIA)
                        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                        .build();

                mSoundPool = new SoundPool.Builder()
                        .setMaxStreams(5)
                        .setAudioAttributes(audioAttributes)
                        .build();
            } else { // 5.0 以前
                mSoundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 5);  // 创建SoundPool
            }
        }
        soundID.put("bomb_explosion",mSoundPool.load(this,R.raw.bomb_explosion,1));
        soundID.put("bullet",mSoundPool.load(this,R.raw.bullet,1));
        soundID.put("bullet_hit",mSoundPool.load(this,R.raw.bullet_hit,1));
        soundID.put("game_over",mSoundPool.load(this,R.raw.game_over,1));
        soundID.put("get_supply",mSoundPool.load(this,R.raw.get_supply,1));
    }

    @Override
    public IBinder onBind(Intent intent){
//        playMusic();
        return new MyBinder();
    }

    public class MyBinder extends Binder {
        public void playBgm(){
            if(player != null){
                stopMusic();
            }
            try {
                player = new MediaPlayer();
                player.setDataSource(getAssets().openFd("bgm.wav"));
                player.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            player.setLooping(true);
            player.start();
        }

        public void playBgmBoss(){
            if(player != null){
                stopMusic();
            }
            try {
                player = new MediaPlayer();
                player.setDataSource(getAssets().openFd("bgm_boss.wav"));
                player.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            player.setLooping(true);
            player.start();
        }
        public void playBombExplosion(){
            mSoundPool.play(soundID.get("bomb_explosion"), 0.2f, 0.2f, 0, 0, 1);
        }
        public void playBullet(){
            mSoundPool.play(soundID.get("bullet"), 1, 1, 0, 0, 1);
        }
        public void playBulletHit(){
            mSoundPool.play(soundID.get("bullet_hit"), 1, 1, 0, 0, 1);
        }
        public void playGameOver(){
            mSoundPool.play(soundID.get("game_over"), 1, 1, 0, 0, 1);
        }
        public void playGetSupply(){
            mSoundPool.play(soundID.get("get_supply"), 1, 1, 0, 0, 1);
        }
        public void stopMusic() {
            if (player != null) {
                player.stop();
                player.reset();//重置
                player.release();//释放
                player = null;
            }
        }
    }

    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        stopMusic();
    }
    /**
     * 停止播放
     */
    public void stopMusic() {
        if (player != null) {
            player.stop();
            player.reset();//重置
            player.release();//释放
            player = null;
        }
    }
}